﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;

namespace ModuleDuoc
{
    public partial class frmDuyetTraLai : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        string _traLai;
        string _duyet;
        public frmDuyetTraLai()
        {
            InitializeComponent();
            this.dgvTraLai.AutoGenerateColumns = false;
        }

        private void frmDuyetTraLai_Load(object sender, EventArgs e)
        {
            loadThuocTraLai();
            cboLoaiPhieu.Text = "Chưa duyệt";
        }
        private void loadThuocTraLai() // Lấy danh sach thuoc khach hang tra lai tu rptBanLe
        {
            if (cboLoaiPhieu.Text == "Chưa duyệt")
            {
                _traLai = "0";
                itmSuaPhieu.Text = "Duyệt phiếu này";
            }
            else            
            {
                _traLai = "1";
                itmSuaPhieu.Text = "Không duyệt phiếu này";
            }
            string sql = "select * from thongke.hien_rpt_TraLai where Duyettralai = '" + _traLai + "' and ngaylap BETWEEN '" + this.date1.Value.ToString("MM/dd/yyyy 00:00:00") + "' AND '" + this.date2.Value.ToString("MM/dd/yyyy 23:59:59") + "' ORDER BY ngaylap";
           
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            this.dgvTraLai.DataSource = dt;
            if (dgvTraLai.Rows.Count > 0)
            {
                itmSuaPhieu.Visible = true;
            }
            else
            {
                itmSuaPhieu.Visible = false;
            }
        }

        private void cboLoaiPhieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadThuocTraLai();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            loadThuocTraLai();
        }

        private void itmSuaPhieu_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn duyệt phiếu này không?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                duyetPhieu();
            }
        }

        private void duyetPhieu()
        {
            if (cboLoaiPhieu.Text == "Chưa duyệt")
            {
                _duyet = "1";
            }
            else
            {
                _duyet = "0";
            }
            string sql = "UPDATE zD_PhieuTraLai SET DuyetPhieu = '" + _duyet + "' WHERE SoHoaDonBanLe ='" + this.dgvTraLai.CurrentRow.Cells["SoHoaDonBanLe"].Value + "'";
            SqlHelper.ExecuteNonQuery(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql);

            string sql2 = "UPDATE zD_PhieuTraLaiCT SET DuyetTraLai = '" + _duyet + "' WHERE SoHoaDonBanLe ='" + this.dgvTraLai.CurrentRow.Cells["SoHoaDonBanLe"].Value + "'";
            SqlHelper.ExecuteNonQuery(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql2);
            MessageBox.Show("Sửa phiếu thành công", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            loadThuocTraLai();
        }

        private void date1_ValueChanged(object sender, EventArgs e)
        {
            loadThuocTraLai();
        }

        private void date2_ValueChanged(object sender, EventArgs e)
        {
            loadThuocTraLai();
        }
    }
}
